<?php
/************************FoxPHP威客系统************************/
/*                                                            */
/*  FoxPHP.Com (C)2006-2016 FoxPHP Inc.                       */
/*  This is NOT a freeware, use is subject to license terms   */
/*  FoxPHP威客系统是商业软件,使用于商业用途请购买授权         */
/*  V3.0 2016.3                                               */
/*  foxphp@aliyun.com                                         */
/*  http://www.foxphp.com                                     */
/*                                                            */
/**************************************************************/

namespace Admin\Controller;
use Think\Controller;
use \Org\Util\String;

class TaskController extends IndexController {
	
	//列表动作-----------------------------------------------------------------------------------------------
	public function indexAction(){
		
		$model=new \Admin\Model\DataModel();
		
		if($_GET['process_order']==0 && IsNum($_GET['process_order'])){
			$sqlwhere=array(' type=%d AND cid=%d ',array('type'=>1,'cid'=>0));
		}else{
			$sqlwhere=array(' type=%d ',array('type'=>1));
		}

		foreach($model->IndexGetData(
				//排序
				array(
					  array('order'=>'id','name'=>'ID'),
					  array('order'=>'subject','name'=>'标题'),
					  array('order'=>'task_mode','name'=>'任务模式'),
					  array('order'=>'uid','name'=>'发布者UID'),
					  array('order'=>'username','name'=>'发布者'),
					  array('order'=>'catid','name'=>'分类'),
					  array('order'=>'money','name'=>'赏金'),
					  array('order'=>'task_number','name'=>'悬赏数量'),
					  array('order'=>'cid','name'=>'是否托管'),
					  array('order'=>'process_order','name'=>'订单流程'),
					  array('order'=>'phone','name'=>'电话'),
					  array('order'=>'top','name'=>'推荐'),
					  array('order'=>'endtime','name'=>'结束时间'),
					  array('order'=>'dateline','name'=>'发布时间'),
					  array('order'=>'process','name'=>'任务状态') 
				),
				//查询
				array(	
					'subject'=>array('subject','search'),
					'id'=>array('id','int'),
					'uid'=>array('uid','int'),
					'cid'=>array('cid','int'),
					'task_mode'=>array('task_mode','int'),
					'process'=>array('process','int'),
					'process_order'=>array('process_order','int'),
					'cityid'=>array('cityid','city'),
					'catid'=>array('catid','catid'),
					'dateline1'=>array('dateline','time'),
					'dateline2'=>array('dateline','time'),
					
				)
				,
				'id DESC'
				,
				20				
				,
				$sqlwhere
				
			) as $key=>$value){
			$this->assign($key,$value);
		}
		
		$category=array();
		if(!empty($_GET['catid'])){
			$catid=intval($_GET['catid']);
			$categoryarr=M('Category')->token(false)->where(" catid=$catid ")->find();
			if(!empty($categoryarr['catid'])){
				$category['category_name_1']=$categoryarr['name'];
				$upid=$categoryarr['upid'];
				$categoryarr=M('Category')->token(false)->where(" catid=$upid ")->find();
				if(!empty($categoryarr['catid'])){
					$category['category_name_2']=$categoryarr['name'];
					$upid=$categoryarr['upid'];
					$categoryarr=M('Category')->token(false)->where(" catid=$upid ")->find();
					if(!empty($categoryarr['catid'])){
						$category['category_name_3']=$categoryarr['name'];
					}
				}
			}
		}
		
		$this->assign('category',$category);
		
		
		$this->display();
	}
	
	
	//稿件列表-----------------------------------------------------------------------------------------------
	public function orderAction(){
		
		$model=new \Admin\Model\DataOrderModel();
			
		$wheresql=null;
		$process=intval($_GET['process']);
		if(IsNum($_GET['process']) && $process==3){
			$wheresql=array(' process=%d AND cid>%d AND seller_is_complete=%d AND seller_is_evaluate=%d  ',array('process'=>1,'cid'=>0,'seller_is_complete'=>0,'seller_is_complete'=>0));
		}	
		if(IsNum($_GET['process']) && $process==4){
			$wheresql=array(' process=%d AND cid>%d AND seller_is_complete=%d AND seller_is_evaluate=%d  ',array('process'=>1,'cid'=>0,'seller_is_complete'=>1,'seller_is_complete'=>0));
		}		

		foreach($model->IndexGetData(
				//排序
				array(
					  array('order'=>'id','name'=>'任务ID'),
					  array('order'=>'oid','name'=>'稿件ID'),
					  array('order'=>'cid','name'=>'中标财务订单号'),
					  array('order'=>'buyer_uid','name'=>'买家ID'),
					  array('order'=>'buyer_username','name'=>'买家用户名'),
					  array('order'=>'seller_uid','name'=>'卖家ID'),
					  array('order'=>'seller_username','name'=>'卖家用户名'),
					  array('order'=>'sid','name'=>'购买服务'),
					  array('order'=>'subject','name'=>'标题'),
					  array('order'=>'dateline','name'=>'发布时间'),
					  array('order'=>'verifytime','name'=>'审核时间'),
					  array('order'=>'money','name'=>'中标赏金'),
					  array('order'=>'content','name'=>'稿件内容'),
					  array('order'=>'process','name'=>'稿件状态') 
				),
				//查询
				array(	
					'id'=>array('id','int'),
					'oid'=>array('oid','int'),
					'cid'=>array('cid','int'),
					'process'=>array('process','int'),
					
					'buyer_uid'=>array('buyer_uid','int'),
					'buyer_username'=>array('buyer_username','string'),
					'seller_uid'=>array('seller_uid','int'),
					'seller_username'=>array('seller_username','string'),

					'dateline1'=>array('dateline','time'),
					'dateline2'=>array('dateline','time'),
					
				)
				,
				'oid DESC'
				,
				20		
				,
				$wheresql				
				
			) as $key=>$value){
			$this->assign($key,$value);
		}

		$this->display();
	}
	
	//评论管理-----------------------------------------------------------------------------------------------
	public function commentAction(){
		$model=new \Admin\Model\DataOrderCommentModel();
		foreach($model->IndexGetData(
				//排序
				array(
					array('order'=>'cid','name'=>'ID'),
					array('order'=>'username','name'=>'用户UID'),
					array('order'=>'username','name'=>'用户名'),
					array('order'=>'content','name'=>'内容'),
					array('order'=>'dateline','name'=>'发布时间')
				),
				//查询
				array(	
					'cid'=>array('cid','int'),
					'uid'=>array('uid','int')
				)
			) as $key=>$value){
			$this->assign($key,$value);
		}

		$this->display();
	}
	
	//删除评论动作-----------------------------------------------------------------------------------------------
	public function commentdeleteAction(){
		
		
		$ids=intval($_GET['id']);
		if(empty($ids)){
			$ids=GetCheckBox($_POST['checkbox']);
		}
		
		if(IsPost('Delete') && !$ids){
			$this->error('对不起，请选择ID操作!',U(MODULE_NAME.'/'.CONTROLLER_NAME.'/comment'),3);
		}	
		
		if(M('DataOrderComment')->token(false)->where("cid IN($ids) ")->delete()){
			$this->error('删除成功!',U(MODULE_NAME.'/'.CONTROLLER_NAME.'/comment'),3);
		}else{
			$this->error('失败!',U(MODULE_NAME.'/'.CONTROLLER_NAME.'/comment'),3);
		}

	}
	
	
	//删除稿件动作-----------------------------------------------------------------------------------------------
	public function orderdeleteAction(){
		
		$id=intval($_GET['id']);
			
		if(empty($id)){
			$message='对不起，请选择ID操作!';
			$this->error($message,U(MODULE_NAME.'/'.CONTROLLER_NAME.'/order'));
		}	
		
		if(M('DataOrder')->token(false)->where("oid=$id")->delete()){
			$this->error('删除成功!',U(MODULE_NAME.'/'.CONTROLLER_NAME.'/order'),3);
		}else{
			$this->error('失败!',U(MODULE_NAME.'/'.CONTROLLER_NAME.'/order'),3);
		}

	}
	
	public function ordereditAction(){
		$id=I('get.id',0,'int');
		$model=new \Admin\Model\DataOrderModel();
		$manage=$model->token(false)->where(" oid=$id ")->find();
		if(!$manage['oid']){
			$this->error('修改数据不存在!',U(MODULE_NAME.'/'.CONTROLLER_NAME.'/order'),3);
		}
		
		if(IsPost('Submit')){
			$data=array();
			$_POST['content']=I('post.content','','strip_tags','',C('S_HTML_ALLOW'));
			$data['content']=String::msubstr(I('post.content','','htmlspecialchars'),0,999,C('S_CHARSET'),false);
			
			if(!empty($_POST['offer'])){
				$data['offer']=intval($_POST['offer']);
			}
			
			if(!empty($_POST['time'])){
				$data['time']=intval($_POST['time']);
			}
			
			$data['SecurityForm']=I('post.SecurityForm');
			
			if(empty($data['content'])){
				$this->error('对不起！请输入内容!',U(MODULE_NAME.'/'.CONTROLLER_NAME.'/order'),3);
				return false;
			}
				
			if(M('DataOrder')->where(" oid=$id ")->save($data)){
				$this->error('修改成功!',U(MODULE_NAME.'/'.CONTROLLER_NAME.'/order'),3);
			}else{
				$this->error('修改数据错误!',U(MODULE_NAME.'/'.CONTROLLER_NAME.'/order'),3);
			}

		}else{
			$id=intval($manage['id']);
			$task=M('Data')->token(false)->where(" id=$id ")->find();
			$this->assign('task',$task);
			$this->assign('draft',$manage);
			$this->assign('oid',$id);
			$this->display('draft');
		}
	}

	//修改动作-----------------------------------------------------------------------------------------------
	public function editAction(){
		$id=I('get.id',0,'int');
		$model=new \Admin\Model\DataModel();
		$manage=$model->token(false)->where(" id=$id ")->find();
		if(!$manage['id']){
			$this->error('修改数据不存在!',U(MODULE_NAME.'/'.CONTROLLER_NAME.'/index'),3);
		}
			
		if(IsPost('Submit')){
			$datamodel=new \Data\Model\DataModel();

			$data=$datamodel->FormatData('task');
			unset($data['uid']);
			unset($data['username']);
			if(C('otoo_open')){
				unset($data['provid']);
				unset($data['cityid']);
				unset($data['distid']);
				unset($data['longitude']);
				unset($data['latitude']);
				unset($data['provname']);
				unset($data['cityname']);
				unset($data['distname']);
			}
			
			$result=$model->EditData($data);
			if($result['id']){
				
				$upload=new \Upload\Model\FileModel();
				$upload->UseFile('sid',$id);
				$file = $upload->token(false)->where(" sid=$id AND thumbcut is not null ")->order('dateline ASC')->find();
				if(!empty($file['thumbcut'])){
					M('Data')->where("id=$id")->save(array('picture'=>$file['thumbcut']));
				}
			
				$this->ajaxReturn(array('title'=>'修改提示','message'=>'修改成功!','status'=>'ok','refresh'=>true));
			}else{
				$this->ajaxReturn(array('title'=>'验证失败','message'=>$model->getError(),'status'=>'error'));
			}
		}else{
			$this->assign('manage',$manage);
			$this->assign('id',$id);
			$this->display('Task:add');
		}
		
	}
	
	
	//稿件操作-----------------------------------------------------------------------------------------------
	public function orderoperateAction(){
		
		
		if(!empty($_POST['Success'])){
			
			$ids=GetCheckBox($_POST['checkbox']);
		  
			if($ids){
				
				M('DataOrder')->token(false)->where(" oid IN($ids) ")->save(array('process'=>1));

				$refresh.='<p>'.$ids.'号稿件设置中标成功！</p>';

			}else{

				$refresh='<p>请选操作数据！</p>';

			}
			
		}elseif(!empty($_POST['Failure'])){
			
			$ids=GetCheckBox($_POST['checkbox']);
		  
			if($ids){
				
				M('DataOrder')->token(false)->where(" oid IN($ids) ")->save(array('process'=>2));

				$refresh.='<p>'.$ids.'号稿件设置不合格成功！</p>';

			}else{

				$refresh='<p>请选操作数据！</p>';

			}
		
		}elseif(!empty($_POST['Close'])){
			   
			$ids=GetCheckBox($_POST['checkbox']);
		  
			if($ids){
				
				M('DataOrder')->token(false)->where(" oid IN($ids) ")->save(array('process'=>0));

				$refresh.='<p>'.$ids.'号稿件设置待选成功！</p>';

			}else{

				$refresh='<p>请选操作数据！</p>';

			}
		}else{
			
			$ids=GetCheckBox($_POST['checkbox']);
		  
			if($ids){
				
				M('DataOrder')->token(false)->where(" oid IN($ids) ")->delete();

				$refresh.='<p>'.$ids.'号稿件删除成功！</p>';

			}else{

				$refresh='<p>请选操作数据！</p>';

			}
		}
		
		$this->error($refresh,U(MODULE_NAME.'/'.CONTROLLER_NAME.'/order'),3);
		
	}
	
	//任务操作-----------------------------------------------------------------------------------------------
	public function operateAction(){
		

		//TOP------------------------------------------ 
		 $tasktop=0;
		 
		 if(!empty($_POST['ListTop'])){  

				$tasktop=1;
		 }
		 
		 if(!empty($_POST['ListTop']) || !empty($_POST['TopClose'])){   
		 
				$ids=GetCheckBox($_POST['checkbox']);
				
				if($ids){
					$idarr=explode(',',$ids);
				  
					foreach($idarr as $id){
						M('Data')->where(" id='$id' ")->save(array('top'=>$tasktop));
					}
				   
					$refresh='设置成功！';
				}else{
					$refresh='没有数据提交！';
				}

		 }elseif(!empty($_POST['Delay'])){   //延期
			  
			   $ids=GetCheckBox($_POST['checkbox']);
				
				if($ids){
				   $idarr=explode(',',$ids);
				   
				   $delaytime=intval(C('task_delay_time'));
				   
				   foreach($idarr as $id){

					   $task=M('Data')->token(false)->where(" id='$id' ")->find();
					   
					   if($task['process']>6 || $task['process']<4){
							 $refresh.='<p>'.$tid.'号任务未审核或已结束关闭.</p>';
					   }else{
							 $endtime=$task['endtime']+24*60*60*$delaytime;
							 
							 M('Data')->where(" id='$id' ")->save(array('endtime'=>$endtime,'process'=>4));
							 
							 $refresh.='<p>'.$tid.'号任务延期'.$delaytime.'天成功！'.FormatDate("Y/m/d H:m:s",$endtime).'结束.</p>';
					   }
				   
				   }
				}else{
					$refresh='没有数据提交！';
				}  
		 
		 }elseif(!empty($_POST['RefundClose'])){
				
				 $ids=GetCheckBox($_POST['checkbox']);
					 
				 if($ids){
					   
					   $task=M('Data')->token(false)->where(" id IN($ids) ")->select();
					   $consume=new \Consume\Model\ConsumeModel();

					   foreach($task as $value){
							  
							  if($value['cid']>0 && $value['process']>7){
								   $refresh.='<p>'.$value['id'].'号任务已经关闭过了！</p>';
							  }elseif($value['cid']>0 && $value['process']>2 && $value['process']<7 ){
								   $consume->TaskRefund($value);
								   $refresh.='<p>'.$value['id'].'号任务操作成功！</p>';
							  }else{
								   $refresh.='<p>'.$value['id'].'号任务未支付不能退款！</p>';
							  }
						   
					   }

				 }else{
					   
					   $refresh='<p>请选操作数据！</p>';
					 
				 }
				 
		   
		  }elseif(!empty($_POST['CloseVerify'])){
			   
				$ids=GetCheckBox($_POST['checkbox']);
			  
				if($ids){
					
					$taskarr=M('Data')->token(false)->where(" id IN($ids) ")->select();
					
					foreach($taskarr as $task){
						  
						   if($task['process']>2 && $task['process']<7 ){
								 $id=$task['id'];
								 M('Data')->where(" id='$id' ")->save(array('process'=>3));
								 
								 $refresh.='<p>'.$task['id'].'号任务取消审核成功！</p>';
						   }else{
								 $refresh.='<p>'.$task['id'].'号任务已结束关闭或未支付赏金不能取消审核！</p>';
						   }
						
					}
							 
				 }else{
					   
					   $refresh='<p>请选操作数据！</p>';
					 
				 }
		  
		   }elseif(!empty($_POST['Verify'])){
				
				$ids=GetCheckBox($_POST['checkbox']);
				
				if($ids){
					
					$taskarr=M('Data')->token(false)->where(" id IN($ids) ")->select();
					
					foreach($taskarr as $task){
						  
						   if($task['process']==3){
							     $id=$task['id'];
								 M('Data')->where(" id='$id' ")->save(array('process'=>4));
								 $refresh.='<p>'.$task['id'].'号任务审核成功！</p>';
						   }else{
								 $refresh.='<p>'.$task['id'].'号任务未支付托管金或已审核过了！</p>';
						   }
						
					}
							 
				 }else{
					   
					   $refresh='<p>请选操作数据！</p>';
					 
				 }	
		   
		  }elseif(!empty($_POST['Close'])){
				
				$ids=GetCheckBox($_POST['checkbox']);
				if($ids){

					M('Data')->token(false)->where(" id IN($ids) ")->save(array('process'=>9));
					
					$refresh='<p>关闭成功！</p>';
					
				}else{
					   
					$refresh='<p>请选操作数据！</p>';
					 
				 }	
					
		  }else{
				$_POST['Delete']=1;
				return $this->deleteAction();
		  
		  }

		$this->error($refresh,U(MODULE_NAME.'/'.CONTROLLER_NAME.'/index'),3);
	}
	
	//删除动作-----------------------------------------------------------------------------------------------
	public function deleteAction(){
		$model=new \Admin\Model\DataModel();
		$result=$model->DeleteData();
		$this->error($result['message'],U(MODULE_NAME.'/'.CONTROLLER_NAME.'/index'),3);
	}
	
}